package com.ydy.service.auth.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import com.ydy.dto.RelationRoleAuthDTO;
import com.ydy.exception.BusinessException;
import com.ydy.exception.ValidateException;
import com.ydy.ienum.EnumAuth;
import com.ydy.mapper.AuthMapper;
import com.ydy.mapper.RelationRoleAuthMapper;
import com.ydy.model.Auth;
import com.ydy.model.RelationRoleAuth;
import com.ydy.service.auth.AuthService;
import com.ydy.utils.ReflexUtil;
import com.ydy.utils.ValidateUtil;
import com.ydy.vo.other.BaseVo;
import com.ydy.vo.other.ResultVo;

import tk.mybatis.mapper.entity.Example;

@Service
@Transactional
public class AuthServiceImpl implements AuthService {

	private final static String BASE_PACK = "com.ydy.controller";
	@Autowired
	private RelationRoleAuthMapper relationRoleAuthMapper;
	@Autowired
	private AuthMapper authMapper;

	@Override
	public BaseVo save(RelationRoleAuthDTO authDTO) {
		if (authDTO == null) {
			throw new NullPointerException();
		}
		// 校验数据有效
		Map<String, String> validateInfo = ValidateUtil.validateEntity(authDTO);
		if (!validateInfo.isEmpty()) {
			throw new ValidateException(validateInfo);
		}
		RelationRoleAuth auth = new RelationRoleAuth();
		auth.setRoleId(authDTO.getRoleId());
		relationRoleAuthMapper.delete(auth);
		Date now = new Date();
		for (String menuName : authDTO.getMenuNames()) {
			auth.setCreateTime(now);
			auth.setMenuName(menuName);
			auth.setRoleId(authDTO.getRoleId());
			relationRoleAuthMapper.insertSelective(auth);
		}
		return new ResultVo();
	}

	@Override
	public Map<String, RelationRoleAuth> listOneRoleAuths(Long roleId) {
		if (roleId == null) {
			throw new NullPointerException();
		}
		Example example = new Example(RelationRoleAuth.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andEqualTo("roleId", roleId);
		List<RelationRoleAuth> list = relationRoleAuthMapper.selectByExample(example);
		Map<String, RelationRoleAuth> map = new HashMap<String, RelationRoleAuth>();
		for (RelationRoleAuth auth : list) {
			map.put(auth.getMenuName(), auth);
		}
		return map;
	}

	@Override
	public Map<String, RelationRoleAuth> listManyRolesAuths(List<Long> roleIds) {
		if (CollectionUtils.isEmpty(roleIds)) {
			throw new NullPointerException();
		}
		Example example = new Example(RelationRoleAuth.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andIn("roleId", roleIds);
		List<RelationRoleAuth> list = relationRoleAuthMapper.selectByExample(example);
		Map<String, RelationRoleAuth> map = new HashMap<String, RelationRoleAuth>();
		for (RelationRoleAuth auth : list) {
			map.put(auth.getMenuName(), auth);
		}
		return map;
	}

	@Override
	public BaseVo refresh() {
		List<Auth> list = ReflexUtil.scanController(BASE_PACK);
		if (CollectionUtils.isEmpty(list)) {
			throw new BusinessException(EnumAuth.AUTH_LIST_EMPTY);
		}
		authMapper.delete(null);
		for (Auth auth : list) {
			authMapper.insertSelective(auth);
		}
		return new ResultVo();
	}

	@Override
	public List<Auth> listAdminAuth() {
		Auth auth = new Auth();
		auth.setAdminFlag(1);
		return authMapper.select(auth);
	}

	@Override
	public List<Auth> listStaffAuth() {
		Auth auth = new Auth();
		auth.setStaffFlag(1);
		return authMapper.select(auth);
	}
}
